/*
 * AdminProductsServlet.java
 * 作用：管理员后台的商品管理控制器，负责获取所有商品列表并转发到JSP页面显示。
 */
package com.campustradingwall.controller.admin;

import com.campustradingwall.dao.ProductDao;
import com.campustradingwall.model.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/products")
public class AdminProductsServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ProductDao productDao = new ProductDao();
        List<Product> productList = productDao.getAllProducts();

        request.setAttribute("products", productList);

        // 检查是否有来自会话的消息（例如，来自删除操作）
        if (request.getSession().getAttribute("message") != null) {
            request.setAttribute("message", request.getSession().getAttribute("message"));
            request.getSession().removeAttribute("message");
        }

        request.getRequestDispatcher("/admin/products.jsp").forward(request, response);
    }
} 